<template>
  <el-container class="home-container">
    <el-header>
      <div class="left">
        <img src="../../assets/logo.png" alt />
        <span>主观题智能阅卷系统</span>
      </div>
      <div class="right">
        <el-avatar icon="el-icon-user-solid"></el-avatar>

        <span>学生A</span>

        <el-button icon="el-icon-switch-button" @click="logout()">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <!-- unique-opened这样写就是默认设置为true，如果要设置其值，必须 :unique-opened="true" 不加冒号会以为是字符串  这是设置菜单只能展开唯一的一项 -->
        <!-- router开启路由属性，实现菜单的跳转，默认跳转地址为二级菜单的index属性值 -->
        <!-- default-active是为实现选中菜单的高亮 -->
        <!-- 设置展开时只保持有一个子菜单展开的设置 -->
        <el-menu
          background-color="white"
          text-color="black"
          active-text-color="white"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span class="sty">{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <!-- saveNavState将被点击的二级菜单的index值存储在sessionStorage -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [
        {
          id: '1',
          authName: '用户期望',
          children: [
            {
              id: '1',
              authName: '镜头1',
              path: 'stu/one'
            },
            {
              id: '2',
              authName: '镜头2',
              path: 'stu/two'
            },
            {
              id: '3',
              authName: '镜头3',
              path: 'stu/three'
            },
            {
              id: '4',
              authName: '镜头4',
              path: 'stu/four'
            },
            {
              id: '5',
              authName: '镜头5',
              path: 'stu/five'
            },
            {
              id: '6',
              authName: '镜头6',
              path: 'stu/six'
            }
          ]
        },
        {
          id: '2',
          authName: '我的答题',
          children: [
            {
              id: '1-1',
              authName: '作业',
              path: 'answer/homework'
            },
            {
              id: '1-2',
              authName: '考试',
              path: 'answer/test'
            }
          ]
        },
        {
          id: '3',
          authName: '我的成绩',
          children: [
            {
              id: '2-1',
              authName: '作业',
              path: 'score/hwork'
            },
            {
              id: '2-2',
              authName: '考试',
              path: 'score/tes'
            }
          ]
        }
      ],
      iconsObj: {
        '1': 'el-icon-pie-chart',
        '2': 'el-icon-pie-chart',
        '3': 'el-icon-pie-chart',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },

      //菜单栏是否折叠
      isCollapse: false,
      // 实现二级子菜单的高亮
      activePath: ''
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$store.commit('clear')
      this.$router.push('/login')
    },
    

    //点击按钮，实现菜单的折叠展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.sty {
  // font-weight: bold;
  font-size: 20px;
}

.home-container {
  height: 100%;
  width: 80%;
  margin-left: 10%;
  background: #f0f0f2;
}

.el-container {
  margin-top: 15px;
}

.el-header {
  background-color: white;
  // margin-left: 10%;
  // width: 80%;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: black;
  font-size: 20px;
  .left {
    display: flex;
    align-items: center;
    margin-left: 10px;
    img {
      width: 5%;
      height: 5%;
    }
    span {
      margin-left: 15px;
    }
  }
  .right {
    display: flex;
    align-items: center;
    span {
      font-size: 15px;
      white-space: nowrap;
    }
  }
}

.el-button {
  margin-left: 50px;
  color: red;
  font-size: 15px;
  // background: white;
  border: none;
}
.el-avatar {
  margin-right: 50px;
}

.el-aside {
  background-color: #f0f0f2;
  height: auto;
  margin-left: 15px;
  .el-menu {
    border-right: none;
    .el-menu-item.is-active {
      background: #f55d54 !important;
    }
  }
}

.el-main {
  background-color: white;
  margin-left: 15px;
  margin-right: 15px;

  // background-image: url("../assets/微信图片_20191012142541.jpg");
  // background-size: 1336px 694.4px;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: white;
  font-size: 10px;
  line-height: 24px;
  color: black;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>